package gao.xiaolei.controller;

import gao.xiaolei.entity.Role;
import gao.xiaolei.entity.Url;
import gao.xiaolei.entity.User;
import org.springframework.web.bind.annotation.*;
import gao.xiaolei.service.UrlService;
import gao.xiaolei.service.UserService;
import gao.xiaolei.service.RoleService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource(name = "RoleService")
    private RoleService roleService;

    @Resource(name = "UserService")
    private UserService userService;

    @Resource(name = "UrlService")
    private UrlService urlService;

    @PutMapping("/role")
    public void updateRoleAuth(@RequestParam(name = "roleId") String role_id,
                               @RequestParam(name = "newUrlJson") String newUrlJson) {
        roleService.updateRoleAuth(role_id, newUrlJson);
    }

    @GetMapping("/role/all")
    public List<Role> getAllRole() {
        return roleService.getAll();
    }

    @PostMapping("/role")
    public Integer addRole(@RequestParam(name = "roleJson") String roleJson) {
        return roleService.addRole(roleJson);
    }

    @GetMapping("/role/auth/{roleId}")
    public List<Url> getRoleAuth(@PathVariable("roleId") String roleId) {
        return roleService.getRoleAuth(roleId);
    }

    @PostMapping("/role/auth/del")
    public void deleteRoleAuth(@RequestParam(name = "roleId") String role_id,
                               @RequestParam(name = "delUrlJson") String delUrlJson) {
        roleService.deleteAuth(role_id, delUrlJson);
    }

    @GetMapping("/role/addAuth/{roleId}")
    public List<Url> getRoleAddAuth(@PathVariable("roleId") String roleId) {
        return roleService.getCanAddAuth(roleId);
    }

    @PostMapping("/role/del")
    public void deleteRole(@RequestParam(name = "roleIdJson") String roleIdJson) {
        roleService.deleteRole(roleIdJson);
    }

    @GetMapping("/user/all")
    public List<User> getAllUser() {
        return userService.getAll();
    }

    @PostMapping("/user")
    public Integer addUser(@RequestParam(name = "newUserJson") String newUserJson) {
        return userService.addUserForAdmin(newUserJson);
    }

    @PutMapping("/user")
    public Integer updateUser(@RequestParam(name = "newUserJson") String newUserJson) {
        return userService.updateUser(newUserJson);
    }

    @PostMapping("/user/del")
    public void deleteUser(@RequestParam(name = "idJson") String idJson) {
        userService.deleteUser(idJson);
    }

    @GetMapping("/user/roles/{userId}")
    public List<Role> getUserRole(@PathVariable("userId") String userId) {
        return userService.getRoles(userId);
    }

    @GetMapping("/url/all")
    public List<Url> getAllUrl() {
        return urlService.getAll();
    }

    @PutMapping("/url")
    public void updateUrl(@RequestParam(name = "id") String id,
                          @RequestParam(name = "url") String url,
                          @RequestParam(name = "detail") String detail,
                          @RequestParam(name = "readOnly", required = false) Integer readOnly) {
        urlService.updateUrl(id, url, detail, readOnly);
    }

    @PostMapping("/url/del")
    public void deleteUrl(@RequestParam(name = "urlJson") String urlJson) {
        urlService.deleteUrl(urlJson);
    }
}
